﻿namespace Sidvall.Data
{
    public class FieldValueItem
    {
        #region Public Members

        public string FieldName { get; set; }
        public string Value { get; set; }
        public DataType ValueDataType { get; set; }
        public bool ValueIsArray { get; set; }

        #region GetFieldValue

        private object _FieldValue;
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
        public object GetFieldValue()
        {
            if (!this.ValueIsCreated)
            {
                if (!this.ValueIsArray)
                    _FieldValue = Sidvall.Serialization.SerializerManager.Current.Deserialize(this.Value, this.ValueDataType);
                else
                    _FieldValue = Sidvall.Serialization.SerializerManager.Current.DeserializeArray(this.Value, this.ValueDataType);
                this.ValueIsCreated = true;
            }
            return _FieldValue;
        }

        #endregion

        #endregion
        #region Private Members

        private bool ValueIsCreated { get; set; }

        #endregion
        #region Constructors

        public FieldValueItem()
        {
        }
        public FieldValueItem(string fieldName, string value, DataType valueDataType, bool valueIsArray)
        {
            this.ValueIsCreated = false;
            this.FieldName = fieldName;
            this.Value = value;
            this.ValueDataType = valueDataType;
            this.ValueIsArray = valueIsArray;
        }

        #endregion
    }
}
